﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace GeekShop
{
    class PhysicalColor
    {
        #region Public Static Methods
        public static int GetWaveLengthFromColor(Color color)
        {
            return 0;
        }
        public static Color GetColorFromWaveLength(int wavelength)
        {
            double gamma = 1.00, blue, green, red, factor;
            int intensityMax = 255;

            if (wavelength >= 350 && wavelength <= 439)
            {
                red = -(wavelength - 440d) / (440d - 350d);
                green = 0.0;
                blue = 1.0;
            }
            else if (wavelength >= 440 && wavelength <= 489)
            {
                red = 0.0;
                green = (wavelength - 440d) / (490d - 440d);
                blue = 1.0;
            }
            else if (wavelength >= 490 && wavelength <= 509)
            {
                red = 0.0;
                green = 1.0;
                blue = -(wavelength - 510d) / (510d - 490d);
            }
            else if (wavelength >= 510 && wavelength <= 579)
            {
                red = (wavelength - 510d) / (580d - 510d);
                green = 1.0;
                blue = 0.0;
            }
            else if (wavelength >= 580 && wavelength <= 644)
            {
                red = 1.0;
                green = -(wavelength - 645d) / (645d - 580d);
                blue = 0.0;
            }
            else if (wavelength >= 645 && wavelength <= 780)
            {
                red = 1.0;
                green = 0.0;
                blue = 0.0;
            }
            else
            {
                red = 0.0;
                green = 0.0;
                blue = 0.0;
            }

            if (wavelength >= 350 && wavelength <= 419)
            {
                factor = 0.3 + 0.7 * (wavelength - 350d) / (420d - 350d);
            }
            else if (wavelength >= 420 && wavelength <= 700)
            {
                factor = 1.0;
            }
            else if (wavelength >= 701 && wavelength <= 780)
            {
                factor = 0.3 + 0.7 * (780d - wavelength) / (780d - 700d);
            }
            else
            {
                factor = 0.0;
            }

            return Color.FromArgb(
                PhysicalColor.AdjustFactor(red, factor, intensityMax, gamma),
                PhysicalColor.AdjustFactor(green, factor, intensityMax, gamma),
                PhysicalColor.AdjustFactor(blue, factor, intensityMax, gamma)
                );
        }
        #endregion

        #region Private Static Methods
        private static int AdjustFactor(double color, double factor, int intensityMax, double gamma)
        {
            if (color == 0.0)
            {
                return 0;
            }
            return (int)Math.Round(intensityMax * Math.Pow(color * factor, gamma));
        }
        #endregion
    }
}
